'use strict';

angular.module('app')
    .controller('blogrollController', ['$rootScope', '$scope', '$http', '$state','$timeout',
        function($rootScope, $scope, $http, $state, $timeout) {
            $scope.title = '友情管理列表';
            $scope.param = { };
            $scope.loading = false;

            $scope.search = function (pageNum) {
                if(pageNum==1){
                    $scope.param.pageNum = 1;
                }
                $scope.param.type = 1;//只显示友情链接
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name + '/blogroll/list',
                    data: $scope.param
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.businessLicenses = [];
            $scope.preview = function(businessLicense){
                if (businessLicense != null && businessLicense != '') {
                    $scope.businessLicenses = businessLicense.split('|');
                }
                $scope.firstApply = true;
            }

            $scope.oper = function (id,$event) {
                $.ajax({
                    url : $scope.app.project_name + '/blogroll/disable',
                    data: {'id': id, 'isEnable' : $event.target.checked?'Y':'N'}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.param.keyword= null;
                        $scope.search();
                    } else {
                        $scope.msg = result.msg;
                    }
                });
            }

            // 拖拽排序
            $scope.editSort=function ($event) {
                if($scope.param.type==''||$scope.param.type==null){
                    $scope.alertModal({'content':'请选择类型!'});
                    return;
                }
                $scope.search();
                $("#blogrollList").sortable({opacity: 0.5,//拖动的透明度
                    revert: true, //缓冲效果
                    cursor: 'move', //拖动的时候鼠标样式
                    connectWith: ".column", scrollSpeed: 40});
                $("#blogrollList").disableSelection();

                for (var i = 0; i < $scope.pageInfo.records.length; i++) {
                    $(".editSortBtn").hide();
                    $(".saveSortBtn").show();
                }
            }

            $scope.delete = function (id) {
                $.ajax({
                    url : $scope.app.project_name+'/blogroll/delete',
                    data: {'id': id}
                }).then(function(result) {
                    $scope.loading = false;
                    $scope.alertModal({"content":"操作成功"});
                    if (result.httpCode != 200) {
                        $scope.alertModal(result.msg);
                    }
                    $scope.search();
                });
            }

            $scope.search();
            //重置按钮的操作
            $scope.clearSearch = function() {
                $scope.param.keyword= null;
                $scope.param={};//将输入的数据清空
                $scope.search();
            }

            // 翻页
            $scope.pagination = function (page) {
                $scope.param.pageNum=page;
                $scope.search();
            };

            $scope.saveSort=function ($event) {
                var batchUpdates = new Array();//声明一个数组来保存多个的id值
                var sortArray = new Array();//声明一个数组来保存多个序号的值
                for (var j = 0; j < $scope.pageInfo.records.length; j++) {
                    var tr = $(".sort  tbody tr")[j];
                    var id = $($(tr).find("td")[1]).html();
                    var sort = j + 1;
                    sortArray[j] = sort;//用来存放序号
                    var batchUpdate = id + "-" + sort;
                    // alert(batchUpdate);
                    batchUpdates[j] = batchUpdate;
                }
                $.ajax({
                    url: $scope.app.project_name + '/blogroll/saveSort',
                    data: {'ids': batchUpdates},
                    traditional: true
                }).then(function (result) {
                    if (result.httpCode == 200) {
                        $scope.alertModal({"content": "操作成功"});
                        $(".editSortBtn").show();
                        $("#sortId").show();
                        $(".saveSortBtn").hide();
                        $("#blogrollList").sortable('destroy');//解除拖拽
                        $timeout(function () {
                            $scope.search();
                        }, 1000);
                    } else {
                        $scope.msg = result.msg;
                    }
                });
            }
    } ]);